package com.snake.springboot.custom.service.port.adapter.listener;

import com.snake.springboot.custom.api.model.base.CustStrategySumModel;
import com.snake.springboot.custom.api.model.login.AccountCacheDelModel;
import com.snake.springboot.custom.api.request.auth.AuthorizationDefaultSave;
import com.snake.springboot.custom.service.port.adapter.event.*;
import com.snake.springboot.custom.service.port.adapter.feign.AuthFeignClient;
import org.snake.common.exception.LoginException;
import org.snake.common.result.ResponseResult;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class AuthListener {
    @Resource
    private AuthFeignClient authFeignClient;


    @EventListener
    public void verifyImage(VerifyImageEvent evt){
        ResponseResult<Boolean> result = authFeignClient.verifyImage(evt.getData());
        if (Boolean.TRUE.equals(result.getData())){
            return;
        }
        throw new LoginException(result.getMsg());
    }

    @EventListener
    public void verifyTextCode(VerifyTextCodeEvent evt){
        ResponseResult<Boolean> result = authFeignClient.verifyTextCode(evt.getData());
        if (Boolean.TRUE.equals(result.getData())){
            return;
        }
        throw new LoginException(result.getMsg());
    }

    @EventListener
    public void delAccCache(AuthDelAccCacheEvt evt){
        AccountCacheDelModel model = new AccountCacheDelModel(evt.getData());
        authFeignClient.delAccCache(model);
    }


    @EventListener
    public void strategySum(AuthStrategyEvent evt){
        ResponseResult<List<CustStrategySumModel>> result =authFeignClient.strategySum(evt.getData());
        evt.setResult(result.getData());
    }

    @EventListener
    public void authorizationCustomDefault(AuthorizationDefaultCustomEvent event){
        List<String> systemIds = new ArrayList<>();
        systemIds.add("merchantManagement");
        AuthorizationDefaultSave model = new AuthorizationDefaultSave(event.getCustomId(),systemIds);
        authFeignClient.authorizationDefault(model);
    }

    @EventListener
    public void authStrategyBuy(AuthStryBuyEvent evt){
        authFeignClient.buyAuthorInner(evt.getData());
    }

    @EventListener
    public void AuthStrategyCancel(AuthStryRetlEvent evt){
        authFeignClient.cancelSomeAuthorInner(evt.getData());
    }


}
